package com.Yourweibo.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class UserDatabase extends SQLiteOpenHelper {
	private static String DATA_BASE_NAME="yi_db";
	private static String TABLE_NAME="user_list_tb";
	private static String _ID="_id";
	public static String FIELD_1="access_token";
	public static String FIELD_2="access_secret";
	public static String FIELD_3="user_name";
	public static String FIELD_4="user_id";
	private static int VESION=3;

	 

	public UserDatabase(Context context) {
		super(context, DATA_BASE_NAME, null, VESION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		String sql="create table "+TABLE_NAME+"("+_ID+" integer primary key autoincrement"+","
 
		+FIELD_1+" TEXT"+","
		+FIELD_2+" TEXT"+","
		+FIELD_3+" TEXT"+","
		+FIELD_4+" INTEGER"
		+")";
		System.out.println("table:"+TABLE_NAME+"is created");
		db.execSQL(sql);
		
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		String sql="drop table if exists "+TABLE_NAME;
	    db.execSQL(sql);
        onCreate(db);

	}
	public  Cursor selectALL(){
		SQLiteDatabase db=this.getReadableDatabase();
		//.query(String  table, String[] columns, String  selection, String[] selectionArgs, String  groupBy, String  having, String  orderBy)
	    Cursor cursor=db.query(TABLE_NAME,new String []{_ID,FIELD_1,FIELD_2,FIELD_3,FIELD_4}, null, null, null, null,  " "+_ID+" desc");
       // print db infor======>
	    System.out.println("Table "+TABLE_NAME+"information:");
	    System.out.println("RowCount: "+cursor.getCount());
 
	    System.out.println("===================="+TABLE_NAME+"====================");
	    System.out.println("_id"+"     "+FIELD_1+"     "+FIELD_2+"     "+FIELD_3+"     "+FIELD_4);
	   
	    if(cursor.getCount()!=0){
	     	cursor.moveToFirst();
		  while(!cursor.isAfterLast()){
		     	 System.out.println(cursor.getInt(0)+" | "+cursor.getString(1)+" | "+cursor.getString(2)+" | "+cursor.getString(3)+" | "+cursor.getInt(4)+" | ");
		     	 System.out.println("------------------------------------------------------------------------------------------");
		     	  cursor.moveToNext();
	    	
	    }
	    	
	   
	    }
	    cursor.moveToFirst();
	    
	    
	    
	    
	    
	    return cursor;
		
	}
	public void insert(String Value[],long uid){
		
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues cv=new ContentValues();
		cv.put(FIELD_1, Value[0]);
		cv.put(FIELD_2, Value[1]);
		cv.put(FIELD_3, Value[2]);
		cv.put(FIELD_4, uid);
		db.insert(TABLE_NAME,null, cv);
		System.out.println("insert successed!");
		
	}
	public void delete(int id){
		
		SQLiteDatabase db=this.getWritableDatabase();
        String where=_ID+"=?";
        String[] whereValue={Integer.toString(id)};
        db.delete(TABLE_NAME, where, whereValue);
		
	};
	public void closeDB(){
		
		this.close();
	}

}
